package com.ruoyi.logistics.service.impl;

import java.util.List;

import com.ruoyi.logistics.domain.po.ExplicitLogistics;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.logistics.domain.Info;
import com.ruoyi.logistics.mapper.LogisticsMapper;
import com.ruoyi.logistics.domain.Logistics;
import com.ruoyi.logistics.service.ILogisticsService;

import javax.annotation.Resource;

/**
 * 物流信息Service业务层处理
 * 

 * @date 2023-07-23
 */
@Service
public class LogisticsServiceImpl implements ILogisticsService 
{
    @Resource
    private LogisticsMapper logisticsMapper;

    /**
     * 查询物流信息
     * 
     * @param id 物流信息主键
     * @return 物流信息
     */
    @Override
    public Logistics selectLogisticsById(Long id)
    {
        return logisticsMapper.selectLogisticsById(id);
    }

    /**
     * 查询完整物流信息
     *
     * @param id 物流信息主键
     * @return 物流信息
     */
    @Override
    public ExplicitLogistics selectExplicitLogisticsById(Long id)
    {
        return logisticsMapper.selectExplicitLogisticsById(id);
    }

    /**
     * 查询完整物流信息
     *
     * @param id 物流信息主键(用于级联选择器)
     * @return 物流信息
     */
    @Override
    public ExplicitLogistics selectExplicitLogisticsByIdCascader(Long id)
    {
        return logisticsMapper.selectExplicitLogisticsByIdCascader(id);
    }

    /**
     * 查询完整物流信息
     *
     * @param logsNumber 物流信息单号
     * @return 物流信息
     */
    @Override
    public ExplicitLogistics selectExplicitLogisticsByLogsNumber(String logsNumber)
    {
        return logisticsMapper.selectExplicitLogisticsByLogsNumber(logsNumber);
    }

    /**
     * 查询物流信息列表
     * 
     * @param logistics 物流信息
     * @return 物流信息
     */
    @Override
    public List<Logistics> selectLogisticsList(Logistics logistics)
    {
        return logisticsMapper.selectLogisticsList(logistics);
    }

    /**
     * 新增物流信息
     * 
     * @param logistics 物流信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLogistics(Logistics logistics)
    {
        int rows = logisticsMapper.insertLogistics(logistics);
        insertInfo(logistics);
        return rows;
    }

    /**
     * 修改物流信息
     * 
     * @param logistics 物流信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLogistics(Logistics logistics)
    {
        logisticsMapper.deleteInfoByLogisticsId(logistics.getId());
        insertInfo(logistics);
        return logisticsMapper.updateLogistics(logistics);
    }

    /**
     * 批量删除物流信息
     * 
     * @param ids 需要删除的物流信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLogisticsByIds(Long[] ids)
    {
        logisticsMapper.deleteInfoByLogisticsIds(ids);
        return logisticsMapper.deleteLogisticsByIds(ids);
    }

    /**
     * 删除物流信息信息
     * 
     * @param id 物流信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLogisticsById(Long id)
    {
        logisticsMapper.deleteInfoByLogisticsId(id);
        return logisticsMapper.deleteLogisticsById(id);
    }

    /**
     * 新增物流详细信息信息
     * 
     * @param logistics 物流信息对象
     */
    public void insertInfo(Logistics logistics)
    {
        List<Info> infoList = logistics.getInfoList();
        Long id = logistics.getId();
        if (StringUtils.isNotNull(infoList))
        {
            List<Info> list = new ArrayList<Info>();
            for (Info info : infoList)
            {
                info.setLogisticsId(id);
                list.add(info);
            }
            if (list.size() > 0)
            {
                logisticsMapper.batchInfo(list);
            }
        }
    }
}
